package gin

import (
	"github.com/gin-gonic/gin"
	"lab-service/api/handler"
	"lab-service/client"
)

func Router() *gin.Engine {
	// setup Account Service Client
	account := &handler.Account{
		UserClient:     client.UserClient(),
		SecurityClient: client.SecurityClient(),
	}
	// setup Security Service Client
	security := &handler.Security{
		SecurityClient: client.SecurityClient(),
	}
	// setup User Service Client
	user := &handler.User{
		UserClient:     client.UserClient(),
		SecurityClient: client.SecurityClient(),
	}
	// setup Post Service Client
	post := &handler.Post{
		PostClient: client.PostClient(),
	}
	// setup Message Service Client
	message := &handler.Message{
		MessageClient: client.MessageClient(),
	}
	// setup Notice Service Client
	notice := &handler.Notice{
		NoticeClient: client.NoticeClient(),
	}

	// Create RESTful handler (using Gin)
	router := gin.Default()
	router.Use(security.Cors)

	api := router.Group("api")
	{
		api.POST("login", account.Login)
		api.POST("register", account.Register)
	}

	apiUser := api.Group("user")
	{
		apiUser.Use(security.VerifyJwt)
		apiUser.GET("me", user.Me)
		apiUser.Use(security.RoleAdmin)
		apiUser.POST("page", user.Page)
	}

	apiPost := api.Group("post")
	{
		apiPost.POST("one", post.One)
		apiPost.POST("page", post.Page)
		apiPost.Use(security.VerifyJwt)
		apiPost.GET("count/me", post.CountMe)
		apiPost.POST("page/me", post.PageMe)
		apiPost.POST("create", post.Create)
		apiPost.Use(security.RoleAdmin)
		apiPost.POST("page/all", post.PageAll)
		apiPost.POST("toggle", post.Toggle)
	}

	apiMsg := api.Group("msg")
	{
		apiMsg.POST("one", message.One)
		apiMsg.POST("page", message.Page)
		apiMsg.Use(security.VerifyJwt)
		apiMsg.POST("create", message.Create)
		apiMsg.GET("count/me", message.CountMe)
		apiMsg.POST("page/me", message.PageMe)
		apiMsg.Use(security.RoleAdmin)
		apiMsg.POST("page/all", message.PageAll)
		apiMsg.POST("toggle", message.Toggle)
	}

	apiNotice := api.Group("notice")
	{
		apiNotice.POST("one", notice.One)
		apiNotice.POST("page", notice.Page)
		apiNotice.Use(security.VerifyJwt)
		apiNotice.Use(security.RoleAdmin)
		apiNotice.POST("create", notice.Create)
		apiNotice.POST("page/all", notice.PageAll)
		apiNotice.POST("toggle", notice.Toggle)
	}

	apiToken := api.Group("token")
	{
		apiToken.Use(security.VerifyJwt)
		apiToken.GET("logout", security.Logout)
		apiToken.GET("refresh", security.RefreshJwt)
	}

	return router
}
